package yxy.game.pm2.command.union.tech;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.player.UnionPlayer;
import yxy.game.pm2.module.union.player.tech.UnionPlayerTech;

import java.util.Collection;

/** 联盟科技 联盟科技重置预览 */
@MessageType(proto = Command.UnionTechResetReview, classtype = UnionMessage.UnionTechResetReviewRequest.class)
public class CMD_UnionTechResetReview extends Command<UnionMessage.UnionTechResetReviewRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionMessage.UnionTechResetReviewRequest request) {
		String playerCode = messageSource.getTag();
		int dataId = request.getDataId();

		UnionMessage.UnionTechResetReviewResponse.Builder response = UnionMessage.UnionTechResetReviewResponse.newBuilder();

		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			send(messageSource, response, result);
			return;
		}
		UnionPlayer unionPlayer = entry.getUnionPlayer();
		UnionPlayerTech playerTech = unionPlayer.getTech();

		Currency cost = playerTech.resetcost();
		Collection<Currency> back = playerTech.resetback(dataId);
		if (back == null) {
			send(messageSource, response, Result.ParamError);
			return;
		}

		response.setCost(new CurrencyHandle().packPoint(cost));
		response.addAllBack(new CurrencyHandle().packItems(back));

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, UnionMessage.UnionTechResetReviewResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}